//
//  SwiftUIView_Popover.swift
//  SwiftUIPractice
//
//  Created by 不知道隔了多少天 (external - Project) on 2024/9/16.
//

//sheets
//animations
//transitions


import SwiftUI

struct SwiftUIView_Popover: View {
    
    @State var showNewScreen: Bool = false
    
    
    var body: some View {
        
        ZStack {
            Color.orange.edgesIgnoringSafeArea(.all)
         
            VStack{
                Button("按钮") {
                    showNewScreen.toggle()
                }.font(.largeTitle)
                
                Spacer()
            }
            
            //方案1 使用表单 sheets
//            .sheet(isPresented: $showNewScreen) {
//                NewScreen()
//            }
            
            //方案2 使用过度 transitions
//            ZStack {  //加这种东西是为了让 第2个页面在消失的时候有动画效果
//                if showNewScreen {
//                    NewScreen(showNewScreen: $showNewScreen).padding(.top, 100).transition(.move(edge: .bottom)).animation(.spring)
//                }
//                
//            }.zIndex(2)
            
            
            //方案3 使用动画 animations
            NewScreen(showNewScreen: $showNewScreen).padding(.top, 100).offset(y: showNewScreen ? 0 : UIScreen.main.bounds.height)
                .animation(.spring)
            
            
            
        }
    }
}

struct NewScreen: View {
    
    @Environment(\.presentationMode) var presentationMode
    @Binding var showNewScreen: Bool
    
    var body: some View {
        ZStack (alignment: .topLeading){
            Color.purple.edgesIgnoringSafeArea(.all)
            
            Button {
                //第一种方案的关闭
                presentationMode.wrappedValue.dismiss()
                //第二种方案的关闭
                showNewScreen.toggle()
                //
                
            } label: {
                Image(systemName: "xmark").foregroundStyle(.white).font(.largeTitle)
                    .padding(20)
            }

            
        }
    }
}


#Preview {
    SwiftUIView_Popover()
//    NewScreen()
}
